<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8">
    <title>用户管理</title>

    <link rel="stylesheet" th:href="@{/layui/css/layui.css}" media="all"/>
    <link rel="stylesheet" th:href="@{/css/public.css}" media="all"/>
    <link rel="stylesheet" th:href="@{/layui_ext/dtree/dtree.css}" media="all"/>
    <link rel="stylesheet" th:href="@{/layui_ext/dtree/font/dtreefont.css}" media="all"/>
    <style>
        input#search_deptTree_select_input_id {
            border-radius: 10px;
            height: 30px;
            margin-top: 4px;
        }
        input#deptTree_select_input_id {
            border-radius: 10px;
            height: 30px;
            margin-top: 4px;
        }
        input#leaderdeptTree_select_input_id {
            border-radius: 10px;
            height: 30px;
            margin-top: 4px;
        }
        input.layui-input.layui-unselect {
            border-radius: 10px;
            height: 30px;
            margin-top: 4px;
        }
    </style>
</head>
<body>
<!--查询条件-->
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 15px;">
    <legend>搜索条件</legend>
</fieldset>
<form action="" method="post" id="searchFrm" lay-filter="searchFrm" class="layui-form">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-inline">
                <input type="text" name="username" id="name" class="layui-input input-radius" placeholder="请输入用户名">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">手机号</label>
            <div class="layui-input-inline">
                <input type="text" name="phone" class="layui-input input-radius"
                       placeholder="请输入用户地址">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block" style="text-align: center;padding-right: 15%;">
            <button type="button" class="layui-btn layui-btn-sm layui-btn-radius" lay-submit="" lay-filter="doSearch"><i
                    class="layui-icon layui-icon-search layui-icon-normal"></i>查询
            </button>
            <button type="reset" onclick="javascript:document.getElementById('search_deptid').value=''"
                    class="layui-btn layui-btn-sm layui-btn-radius layui-btn-warm"><i
                    class="layui-icon layui-icon-refresh"></i><span>重置</span>
            </button>
        </div>
    </div>
</form>

<!--数据表格-->
<div style="padding-bottom: 5%;">
    <table class="layui-hide" id="userTable" lay-filter="userTable"></table>
    <div id="userToolBar" style="display: none">
        <button type="button" lay-event="add"  class="layui-btn layui-btn-sm layui-btn-normal layui-btn-radius">
            <i class="layui-icon layui-icon-add-1"></i>添加用户
        </button>
    </div>
    <div id="userRowBar" style="display: none;">
        <button type="button" lay-event="update"  class="layui-btn layui-btn-xs layui-btn-radius"><i
                class="layui-icon layui-icon-edit"></i>编辑
        </button>
        <button type="button" lay-event="delete"  class="layui-btn layui-btn-xs layui-btn-danger layui-btn-radius"><i
                class="layui-icon layui-icon-delete"></i>删除
        </button>
        <button type="button" lay-event="resetPwd"  class="layui-btn layui-btn-xs layui-btn-danger layui-btn-radius"><i
                class="layui-icon layui-icon-delete"></i>重置密码
        </button>
        <button type="button" lay-event="selectRole"  class="layui-btn layui-btn-xs layui-btn-danger layui-btn-radius"><i
                class="layui-icon layui-icon-delete"></i>分配角色
        </button>
    </div>
</div>

<!--添加和修改弹出层-->
<div id="addOrUpdateDiv" style="display: none;padding: 10px;padding-right: 5%">
    <form action="" method="post" class="layui-form" id="dataFrm" lay-filter="dataFrm">
        <div class="layui-form-item">
            <label class="layui-form-label">所属学院</label>
            <div class="layui-input-block">
                <select name="xueYuanId" id="xueyuan" lay-filter="aihao" lay-verify="required">
                    <option value=""></option>
                    <option value="0">写作</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">所属班级</label>
            <div class="layui-input-block">
                <select name="banJiId" id="banji" lay-filter="aihao" lay-verify="required">
                    <option value=""></option>
                    <option value="0">写作</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">任课老师</label>
            <div class="layui-input-block">
                <select name="teacherId" id="teacher" lay-filter="aihao" lay-verify="required">
                    <option value=""></option>
                    <option value="0">写作</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">用户名称</label>
                <div class="layui-input-block">
                    <input type="hidden" name="id">
                    <input type="text" name="username" lay-verify="required" autocomplete="off"
                           class="layui-input input-radius" placeholder="请输入用户名称">
                </div>
            </div>
            <div class="layui-inline" style="padding-left: 4%">
                <label class="layui-form-label">登陆密码</label>
                <div class="layui-input-block">
                    <input type="text" name="password" id="loginname" lay-verify="required" autocomplete="off"
                           class="layui-input input-radius" placeholder="请输入登陆密码">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">用户地址</label>
                <div class="layui-input-block">
                    <input type="text" name="address" id="address" class="layui-input input-radius" placeholder="请输入用户地址">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">年龄</label>
                <div class="layui-input-block">
                    <input type="text" name="age" id="age" class="layui-input input-radius" placeholder="请输入用户地址">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">身份证</label>
                <div class="layui-input-block">
                    <input type="text" placeholder="请输入用户身份证" class="layui-input input-radius" name="card" id="card">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">手机号</label>
                <div class="layui-input-block">
                    <input type="text" placeholder="请输入手机号" class="layui-input input-radius" name="phone" id="phone">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">用户性别</label>
                <div class="layui-input-block">
                    <input type="radio" name="sex" value="1" title="男" checked="">
                    <input type="radio" name="sex" value="0" title="女">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="text-align: center;padding-right: 7%">
                <button type="button" class="layui-btn layui-btn-radius" lay-submit="" lay-filter="doSubmit"
                        id="doSubmit"><i
                        class="layui-icon layui-icon-search layui-icon-normal"></i>提交
                </button>
                <button type="reset" onclick="javascript:document.getElementById('deptid').value=''" class="layui-btn layui-btn-radius layui-btn-warm"><i
                        class="layui-icon layui-icon-refresh"></i><span>重置</span>
                </button>
            </div>
        </div>
    </form>
</div>

<!--给用户分配角色的弹出层-->
<div style="display: none;padding: 10px;"  id="selectUserRoleDiv">
    <table class="layui-hide" id="roleTable" lay-filter="roleTable"></table>
</div>


<script type="text/javascript" th:src="@{/layui/layui.js}"></script>

<script type="text/javascript">

    //提升数据表格的作用域，因为底下还有一个reloadTable方法
    var tableIns;

    layui.extend({
        dtree: '/layui_ext/dtree/dtree'
    }).use(['jquery', 'form', 'layer', 'laydate', 'table', 'layedit', 'dtree','laydate'], function () {
        var $ = layui.jquery;
        var form = layui.form;
        var layer = layui.layer;
        var table = layui.table;
        var dtree = layui.dtree;
        var laydate = layui.laydate;

        //初始化时间选择器
        laydate.render({
            elem: "#hiredate",
            type: "datetime"
        });

        //初始化表格 加载数据
        tableIns = table.render({
            elem: "#userTable",
            title: "部门数据表格",
            url: "/user/loadAllUser",
            toolbar: "#userToolBar",
            page: true,
            width: 1100,
            height: "full-180",
            cols: [ [
                {field: 'id', title: 'ID', align: 'center', width: '50'},
                {field: 'username', title: '用户名', align: 'center', width: '130'},
                {field: 'password', title: '密码', align: 'center', width: '130'},
                {field: 'age', title: '年龄', align: 'center', width: '130'},
                {field: 'sex', title: '性别', align: 'center', width: '120'},
                {field: 'address', title: '住址', align: 'center', width: '100'},
                {field: 'img', title: '头像', align: 'center', width: '160', templet:function(d){
                        return '<img width=40 height=40 src=localhost:8080'+d.img+ ' />';
                }},
                {field: 'phone', title: '手机', align: 'center', width: '100'},
                {field: 'card', title: '身份证', align: 'center', width: '100'},
                {field: 'banJiName', title: '班级', align: 'center', width: '100'},
                {field: 'xueYuanName', title: '学院', align: 'center', width: '100'},
                {field: 'teacherName', title: '老师', align: 'center', width: '100'},
                {fixed: 'right', title: '操作', toolbar: '#userRowBar', align: 'center', width: '330'}
            ] ],
            done: function (data, curr, count) {
                //不是第一页时，如果当前返回的数据为0那么就返回上一页
                if (data.data.length == 0 && curr != 1) {
                    tableIns.reload({
                        page: {
                            curr: curr - 1
                        }
                    })
                }
            }
        });

        //监控模糊查询按钮事件
        form.on("submit(doSearch)", function (data) {
            tableIns.reload({
                where: data.field,
                page: {
                    curr: 1
                }
            });
            return false;
        });

        //监控工具条事件
        table.on("toolbar(userTable)", function (obj) {
            switch (obj.event) {
                case 'add':
                    openAddLayer();
                    break;
            }
            ;
        });

        //监控行工具条事件
        table.on("tool(userTable)", function (obj) {
            //获取当前行数据
            var data = obj.data;
            switch (obj.event) {
                case 'delete':
                    deleteUser(data);
                    break;
                case 'update':
                    updateUser(data);
                    break;
                case 'resetPwd':
                    resetPwd(data);
                    break;
                case 'selectRole':
                    selectRole(data);
                    break;
            }
            ;
        });

        var mainIndex;
        var url;

        //打开添加弹出层
        function openAddLayer() {
            mainIndex = layer.open({
                type: 1,
                content: $("#addOrUpdateDiv"),
                area: ['800px', '550px'],
                title: '添加用户',
                success: function () {
                    $("#dataFrm")[0].reset();

                    url = "/user/addUser";
                    //初始化下拉列表【学院、班级、老师】
                    //发送一个get请求发送到后台
                    //【学院】
                    $.get("/user/listAllXueYuan",function (res) {
                        var xueyuan = res;
                        var dom_xueyuan=$("#xueyuan");
                        var html = "<option value=''>选择学院</option>";
                        //通过res遍历数据
                        $.each(xueyuan,function (index,item) {
                            //对html进行追加
                            console.log(item.name);
                            html+="<option value='"+item.id+"'>"+item.name+"</option>";
                        });
                        dom_xueyuan.html(html);
                        //通过form进行渲染
                        form.render("select");
                    });
                    //【班级】
                    $.get("/user/listAllBanJi",function (res) {
                        var banji = res;
                        var dom_banji=$("#banji");
                        var html = "<option value=''>选择班级</option>";
                        //通过res遍历数据
                        $.each(banji,function (index,item) {
                            //对html进行追加
                            console.log(item.name);
                            html+="<option value='"+item.id+"'>"+item.name+"</option>";
                        });
                        dom_banji.html(html);
                        //通过form进行渲染
                        form.render("select");
                    });
                    //【老师】
                    $.get("/user/listAllTeacher",function (res) {
                        var teacher = res;
                        var dom_teacher=$("#teacher");
                        var html = "<option value=''>选择任课老师</option>";
                        //通过res遍历数据
                        $.each(teacher,function (index,item) {
                            //对html进行追加
                            console.log(item.name);
                            html+="<option value='"+item.id+"'>"+item.name+"</option>";
                        });
                        dom_teacher.html(html);
                        //通过form进行渲染
                        form.render("select");
                    });
                }
            });
        }

        //打开修改的弹出层
        function updateUser(data) {
            mainIndex = layer.open({
                type: 1,
                content: $("#addOrUpdateDiv"),
                area: ['800px', '550px'],
                title: '修改用户',
                success: function () {
                    //清空原有的数据
                    $("#dataFrm")[0].reset();
                    //初始化下拉列表【学院、班级、老师】
                    //发送一个get请求发送到后台
                    //【学院】
                    $.get("/user/listAllXueYuan",function (res) {
                        var xueyuan = res;
                        var dom_xueyuan=$("#xueyuan");
                        var html = "<option value=''>选择学院</option>";
                        //通过res遍历数据
                        $.each(xueyuan,function (index,item) {
                            //对html进行追加
                            console.log(item.name);
                            html+="<option value='"+item.id+"'>"+item.name+"</option>";
                        });
                        dom_xueyuan.html(html);
                        //通过form进行渲染
                        form.render("select");
                    });
                    //【班级】
                    $.get("/user/listAllBanJi",function (res) {
                        var banji = res;
                        var dom_banji=$("#banji");
                        var html = "<option value=''>选择班级</option>";
                        //通过res遍历数据
                        $.each(banji,function (index,item) {
                            //对html进行追加
                            console.log(item.name);
                            html+="<option value='"+item.id+"'>"+item.name+"</option>";
                        });
                        dom_banji.html(html);
                        //通过form进行渲染
                        form.render("select");
                    });
                    //【老师】
                    $.get("/user/listAllTeacher",function (res) {
                        var teacher = res;
                        var dom_teacher=$("#teacher");
                        var html = "<option value=''>选择任课老师</option>";
                        //通过res遍历数据
                        $.each(teacher,function (index,item) {
                            //对html进行追加
                            console.log(item.name);
                            html+="<option value='"+item.id+"'>"+item.name+"</option>";
                        });
                        dom_teacher.html(html);
                        //通过form进行渲染
                        form.render("select");
                    });
                    //装载新的数据
                    form.val("dataFrm", data);
                    url = "/user/updateUser";
                }
            });
        }

        form.on("submit(doSubmit)", function (data) {
            $.post(url, data.field, function (res) {
                if (res.code == 200) {
                    tableIns.reload();
                }
                layer.msg(res.msg);
                layer.close(mainIndex);
            });
            return false;
        });

        //删除
        function deleteUser(data) {
            layer.confirm('你确定要删除【' + data.username + '】这个用户吗？', {icon: 3, title: '提示'}, function (index) {
                        $.post("/user/deleteUser/"+data.id, function (res) {
                            if (res.code == 200) {
                                tableIns.reload();
                            }
                            layer.msg(res.msg);
                        })
                        layer.close(index);
            });
        }

        //重置密码
        function resetPwd(data) {
            layer.confirm('你确定要重置【' + data.username + '】这个用户的密码吗？', {icon: 3, title: '提示'}, function (index) {
                $.post("/user/resetPwd/"+data.id, function (res) {
                    if (res.code == 200) {
                        tableIns.reload();
                    }
                    layer.msg(res.msg);
                });
                layer.close(index);
            });
        }


        //监听用户名失去焦点事件
        $("#username").on("blur",function () {
            var username = $(this).val();
            $.get("/user/changeChineseToPinyin",{username:username},function (res) {
                $("#loginname").val(res.value);
            })
        });


        //打开分配角色的弹出层
        function selectRole(data) {
            mainIndex = layer.open({
                type:1,
                content:$("#selectUserRoleDiv"),
                area:['650px','400px'],
                title:'给'+data.username+'分配角色',
                btn: ['<span class=layui-icon>&#xe605;确认分配</span>','<span class=layui-icon>&#x1006;取消分配</span>'],
                btnAlign:'c',
                yes: function(index, layero){
                    var checkStatus = table.checkStatus('roleTable');
                    var params = "uid="+data.id;
                    $.each(checkStatus.data,function (index, item) {
                        params+="&ids="+item.id;
                    });
                    $.post("/user/saveUserRole",params,function (res) {
                        layer.msg(res.msg);
                        layer.close(mainIndex);
                    })
                },
                btn2: function(index, layero){
                    //关闭弹出层
                },
                success:function () {
                    initRoleTable(data);
                }
            });
        }


        var roleTableIns;
        //初始化角色列表
        function initRoleTable(data) {
            console.log(data);
            roleTableIns = table.render({
                elem:'#roleTable',
                url:'/user/initRoleByUserId',
                //将用户id传到后台去
                where:{
                    id:data.id
                },
                cols:[ [
                    {type:'checkbox',align:'center'},
                    {field:'id',title:'ID',align:'center'},
                    {field:'name',title:'角色名称',align:'center'},
                    {field:'remark',title:'角色备注',align:'center'}
                ] ]
            });
        }



    });

</script>

</body>
</html>